package com.stylefeng.guns.rest.modular.wz.dao;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import com.stylefeng.guns.rest.modular.wz.service.impl.WzUserAdvServiceImpl;
import com.stylefeng.guns.rest.modular.wz.service.impl.WzUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * 审核数据保存类
 */
@Service
public class WzAdminDao {

    @Autowired
    private WzUserMapper userMapper;
    @Autowired
    private WzUserFinanceRecordMapper userFinanceRecordMapper;
    @Autowired
    private PfFinanceRecordMapper pfFinanceRecordMapper;
    @Autowired
    private WzAdvMapper advMapper;
    @Autowired
    private WzAdvMapMapper advMapMapper;
    @Autowired
    private WzUserAdvServiceImpl userAdvService;
    @Autowired
    private PfFileMapper pfFileMapper;
    @Autowired
    private WzMapMergeMapper mapMergeMapper;
    @Autowired
    private WzMapRebateMapper mapRebateMapper;

    @Transactional
    public boolean saveRedpackAdvMapCheckSuccess(List<WzUserAdv> userAdvs, WzUser user, WzAdvMap advMap, WzAdv adv) {
        boolean f = userAdvService.insertBatch(userAdvs);
        if(f){
            int r = userMapper.updateById(user);
            if(r > 0){
                r = advMapMapper.updateById(advMap);
                if(r > 0){
                    if(adv != null){
                        r = advMapper.insert(adv);
                        if(r > 0){
                            return true;
                        }
                    }
                    return true;
                }
            }
        }
        return false;
    }

    @Transactional
    public boolean saveAdvMapCheckSuccess(WzUser user,WzAdvMap advMap) {
        int r = advMapMapper.updateById(advMap);
        if(r > 0){
            r = userMapper.updateById(user);
            if(r > 0){
                return true;
            }
        }
        return false;
    }

    @Transactional
    public boolean saveAdvMapCheckSuccessRelesseEditors(List<PfFile> newPfFiles, WzAdvMap advMap,WzAdvMap newAdvMap) {
        int r = advMapMapper.updateById(advMap);
        if(r > 0){
            advMapMapper.deleteById(newAdvMap);
            pfFileMapper.delete(new EntityWrapper<PfFile>().eq("target_id",advMap.getAdvMapId().toString()));
            for(PfFile pfFile:newPfFiles){
                pfFile.setTargetId(advMap.getAdvMapId().toString());
                r = pfFileMapper.updateById(pfFile);
                if(r <= 0){
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    @Transactional
    public boolean mapMergeCheck(WzAdvMap advMap, WzMapMerge mapMerge) {
        int r = mapMergeMapper.updateById(mapMerge);
        if(r > 0 && advMap.getIsRecommend() != 1){
            advMap.setIsRecommend(1);
            r = advMapMapper.updateById(advMap);
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean mapRebateCheck(WzAdvMap advMap, WzMapRebate mapRebate) {
        int r = mapRebateMapper.updateById(mapRebate);
        if(r > 0 && advMap.getIsRecommend() != 1 && mapRebate.getType() == 1){
            advMap.setIsRecommend(1);
            r = advMapMapper.updateById(advMap);
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }
}
